'use strict'

const _ = require('lodash')

module.exports = (app) => {
  const { router, controller, middleware } = app
  const { jwtAuth } = middleware.auth
  const { config } = app
  const { web } = controller

  const prefix = `/web`

  const userAuth = jwtAuth({
    jwtName: 'web',
    loginUrl: '/login',
    secret: config.jwt.secret + 'web',
    allowSession: true,
  })

  // 有传入token强制登录
  router.use(`${prefix}`, async (ctx, next) => {
    if (ctx.header.authorization) {
      await userAuth(ctx, next)
    } else {
      await next()
    }
  })

  /**
   * 中间件
   */
  router.use(`${prefix}`, middleware.api) // API 通用中间件

  /**
   * 系统服务
   */
  // router.get(`${prefix}/qrcode`, web.qrcode.createQRCode) // 二维码

  /**
   * 账号
   */
  // router.post(`${prefix}/login`, web.user.login) // 登录

  router.use(`${prefix}`, userAuth)
}
